public class Solution1007 {
    public int minDominoRotations(int[] tops, int[] bottoms) {
        int ans=Integer.MAX_VALUE;
        for (int i=1;i<=6;i++){
            boolean flag=true;
            int ans1=0;
            for (int j=0;j<tops.length;j++){
                if (tops[j]!=i && bottoms[j]==i){
                    ans1++;
                }else if (tops[j]!=i && bottoms[j]!=i){
                    flag=false;
                    break;
                }
            }
            if (!flag){
                continue;
            }
            ans=Math.min(ans,ans1);
            ans1=0;
            for (int j=0;j<tops.length;j++){
                if (tops[j]==i && bottoms[j]!=i){
                    ans1++;
                }
            }
            ans=Math.min(ans,ans1);
        }
        return ans==Integer.MAX_VALUE?-1:ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1007().minDominoRotations(new int[]{2,1,2,4,2,2},new int[]{5,2,6,2,3,2}));
    }
}
